package com.chen.facemanager.service.impl;

import com.chen.facemanager.bean.User;
import com.chen.facemanager.dao.UserDao;
import com.chen.facemanager.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Override
    public User getUserByUserId(Long id) {

        Optional<User> user = userDao.findById(id);
        if (user.isPresent()) {
            return user.get();
        }
        return null;
    }

    @Override
    public User login(String phone, String password) {
        User user = userDao.findByPhoneAndPassword(phone, password);
        return user;
    }

    @Override
    public User reg(String phone, String password,String macid,String androidid,String imei1,String imei2,String imsi1,String imsi2,String deviceinfo) {
        User user = new User();
        user.setPhone(phone);
        user.setPassword(password);
        user.setStatus(0);

        user.setCard_type(0);
        user.setUuid(UUID.randomUUID().toString().replace("-",""));
        user.setContry("中国");
        user.setNation("");
        user.setType(0);
        user.setCreate_time(new Date());

        user.setMacid(macid);
        user.setAndroidid(androidid);
        user.setImei1(imei1);
        user.setImei2(imei2);
        user.setImsi1(imsi1);
        user.setImsi2(imsi2);
        user.setDeviceinfo(deviceinfo);
        return userDao.save(user);
    }

    @Transactional
    @Override
    public boolean saveInfo(Long id, String name, String idNumber, String address, String sex, String photo, String birthday) {
        int i = userDao.updateUser(id, name, idNumber, address, sex, photo, birthday);
        if (i != 1) {
            return false;
        } else {
            return true;
        }
    }

    @Transactional
    @Override
    public boolean updatePhone(String id, String phone) {
        int i = userDao.updatePhone(Long.parseLong(id), phone);
        if (i != 1) {
            return false;
        } else {
            return true;
        }
    }

    @Transactional
    @Override
    public boolean updatePwd(String id, String password) {
        int i = userDao.updatePwd(Long.parseLong(id), password);
        if (i != 1) {
            return false;
        } else {
            return true;
        }
    }

    @Override
    public boolean samePwd(String id, String oldpwd) {
        if (userDao.findByIdAndPassword(Long.parseLong(id),oldpwd) != null){
            return true;
        }
        return false;
    }

    @Transactional
    @Override
    public boolean resetPwd(String phone, String password) {
        if (userDao.resetPwd(phone,password) > 0){
            return true;
        }
        return false;
    }

    @Override
    public List<User> findUsersByPhone(String phone) {
        return userDao.findUsersByPhone(phone);
    }
}
